# coding: utf-8 or # -*- coding: utf-8 -*-
# 提供卡信息查询接口
# 缺点1当query指定列时生成的json有问题
# 缺点2没法自定义增加json
# flask_jwt
import os
import json
import threading
from flask import Flask
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import (create_engine, MetaData, Column, Integer, String)
from sqlalchemy import func
from sqlalchemy.sql.expression import and_, or_
from sqlalchemy import Table, Column, Integer, String, Text, DateTime, MetaData, ForeignKey, Boolean, types, Numeric
from datetime import datetime, date
import hashlib
from sqlca import get_Session
from pos_check import pos_en, token_create, token_check
from card_app_log import app_log

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

Base = declarative_base()

class infor_worker(Base):
    # 表的名字:
    __tablename__ = 'infor_worker'
    # 表的结构:
    C_WORKER_ID = Column(String(30), primary_key=True)
    C_DEPT_ID = Column(String(40))
    C_WORKER_NAME = Column(String(40))
    C_WORKER_POSITION = Column(String(40))
    C_LIMIT = Column(String(40))
    C_PHONE = Column(String(40))
    C_PASSWORD = Column(String(40))
    C_HT_NO = Column(String(40))
    C_ID = Column(String(40))
    C_STATUS = Column(String(40))
    C_COUNTER_ID = Column(String(40))


def get_worker(worker):
    #app_log(worker,'input')
    if worker.find('&') <= 0:
        ls_msg = "输入参数错误1"
        return json.dumps({"code": "34", "error": ls_msg}, ensure_ascii=False)

    if worker.find('=') <= 0:
        ls_msg = "输入参数错误2"
        return json.dumps({"code": "34", "error": ls_msg}, ensure_ascii=False)

    json_list = worker.split('&')
    dt = {}
    for lst in json_list:
        li = lst.split('=')
        dt[li[0]] = li[1]
    di = eval(dt["req_data"])   
    oper = di["operator"]
    pos_no = di["posno"]
    dic_pos =pos_en(pos_no)
    print dic_pos
    if dic_pos['code'] != '10':
        return json.dumps(dic_pos, ensure_ascii=False)
    token = token_create(pos_no,oper)
    session = get_Session()()
    query = session.query(func.count(1)).filter(
        infor_worker.C_WORKER_ID == oper).scalar()
    if query == 0:
        ls_msg = "操作员%s不存在" % oper
        session.close()
        return json.dumps({"code": "32", "error": ls_msg}, ensure_ascii=False)
    query2 = session.query(infor_worker).filter(
        infor_worker.C_WORKER_ID == oper).scalar()
    m = hashlib.md5()
    print query2.C_PASSWORD
    m.update(query2.C_PASSWORD)
    pwd = m.hexdigest()
    print pwd
    if pwd != di["pwd"]:
        ls_msg = "操作员密码%s错误" % oper
        session.close()
        return json.dumps({"code": "33", "error": ls_msg}, ensure_ascii=False)
    session.close()
    return json.dumps({"code": "10", "error": "succeed", "key": token}, ensure_ascii=False)





# print get_worker('req_type=Login&req_gather=1&req_data={"posno":"21323","operator":"70035","pwd":"202cb962ac59075b964b07152d234b70","role":"03"}')
# oper = '2333'
# ls_msg = "操作员%s不存在"%oper
# print json.dumps({"code":"32","error":ls_msg},ensure_ascii=False)

# ensure_ascii=False,indent=2
# http://127.0.0.1:5000/login/req_type=Login&req_gather=1&req_data={"posno":"21323","operator":"70035","pwd":"123","role":"03"}
